/***************************************************************************
 *   Copyright (C) 2007 by Luca Bellonda                                   *
 *   lbell @ tsc4.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
package lb.prove;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.DigitsInputMethod;
import android.text.method.TextInputMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AskUser extends Activity {

	boolean isInputNumber = false;
	int		minValue = 0;
	int		maxValue = 100;
	String	defaultValue;
	String	title=""; 

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
        setContentView(R.layout.input_value);
      
        TextView titleText = (TextView) findViewById(R.id.label_userinput);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            title = extras.getString(PlanetFeedsRead.KEY_TITLE);
            if(null!=title)
            	titleText.setText(title);
            else
            	title = "";
            Integer minVal = extras.getInteger(PlanetFeedsRead.KEY_MINVAL);
            if(null!=minVal)
            	minValue = minVal.intValue();
            Integer maxVal = extras.getInteger(PlanetFeedsRead.KEY_MAXVAL);
            if(null!=maxVal)
            	maxValue = maxVal.intValue();
            Boolean isNumber = extras.getBoolean(PlanetFeedsRead.KEY_ISNUMBER);
            if(null!=isNumber)
            	isInputNumber = isNumber.booleanValue();
            getEdit().setInputMethod(isInputNumber?DigitsInputMethod.getInstance():TextInputMethod.getInstance());
            
            String initalValue = extras.getString(PlanetFeedsRead.KEY_INITIALVALUE);
            if(null!=initalValue)
            	getEdit().setText(initalValue);
            defaultValue = extras.getString(PlanetFeedsRead.KEY_DEFAULTVALUE);
            if(null!=defaultValue)
            	defaultValue = "";
        }
        Button okButton = (Button) findViewById(R.id.userinput_ok);

        okButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	Bundle bundle = new Bundle();
                String result = getEdit().getText().toString();
                boolean isOk = true;
                int retInt=0;
                if((null==result)||(0==result.length()))
                {
                	result = defaultValue;
                } else 
                {
                	if(isInputNumber)
                	{
                		try
                		{
                			Integer value = Integer.decode(result);
                			if( ( value < minValue ) || (value > maxValue ) )
                				throw new Exception();
                			retInt = value.intValue();
                		} catch (Exception e )
                		{
                			isOk = false;
                		}
                	}
                }
                if( isOk )
                {
                	if(isInputNumber)
                    	bundle.putInteger(PlanetFeedsRead.KEY_RETURNVALUE, retInt);
                	else
                		bundle.putString(PlanetFeedsRead.KEY_RETURNVALUE, result);
                	setResult(RESULT_OK, null, bundle);
                	finish();
                } else
                {
                	showAlert(title, getText(R.string.input_notval), getText(R.string.butt_close), null, false, null);
                }
            }
           
        });
	}
	EditText getEdit()
	{
        return (EditText) findViewById(R.id.userinput_entry);
	}
}
